package aufgabe1;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.GregorianCalendar;

/**
 * definition of a Course-object
 * @author Lukas Koell
 * @author David Medenjak
 * @author Johannes Riedmann
 */
public class Course implements Serializable{	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2435616006556314047L;
	
	/** number of course */
	private int courseNr;
	/** title of course */
	private String title;
	/** students can start to subscribe at this date */
	private GregorianCalendar subscribeStart;
	/** subscribing ends at this date */
	private GregorianCalendar subscribeEnd;
	/** students can't unsubscribe after this date */
	private GregorianCalendar unsubscribeEnd;
	/** list of subscribed students */
	private ArrayList<Student> subscribedStudents;

  public Course(int lvaNr, String title, GregorianCalendar subscribeStart, 
		  GregorianCalendar subscribeEnd, GregorianCalendar unsubscribeEnd) {
    this.courseNr = lvaNr;
    this.title = title;
    this.subscribeStart = subscribeStart;
    this.subscribeEnd = subscribeEnd;
    this.unsubscribeEnd = unsubscribeEnd;
    subscribedStudents = new ArrayList<Student>();
  }

  /**
   * Subscribes a student to the course.
   * @param student Student to be added to the course.
   * @param today The actual date.
   * @throws CourseException if no student is specified or he already is subscribed.
   */
  public void subscribeStudent(Student student, GregorianCalendar today) throws CourseException {
    if (student == null) {
      throw new CourseException("No Student set");
    }
    if (!subscribeStart.before(today) || !subscribeEnd.after(today)) {
      throw new CourseException("Only can subscribe during subscription period");
    }
    if (!subscribedStudents.contains(student)) {
      subscribedStudents.add(student);
    } else {
      throw new CourseException("Student is already subscribed");
    }
  }

  /**
   * Unsubscribes a student from the course.
   * @param student Student who unsubscribes.
   * @param today The actual date.
   * @throws CourseException if no student is specified or if he isn't subscribed to the course.
   */
  public void unsubscribeStudent(Student student, GregorianCalendar today) throws CourseException {
    if (student == null) {
      throw new CourseException("No Student set");
    }
    if (!unsubscribeEnd.after(today)) {
      throw new CourseException("Only can unsubscribe before end of unsubscriptions");
    }
    if (!subscribedStudents.remove(student)) {
      throw new CourseException("Student is not subscribed");
    }
  }

  /**
   * Getter-method for title.
   * @return the title of the course
   */
  public String getTitle() {
    return title;
  }
  
  /**
   * Shows how many students have subscribed to this couse.
   * @return number of subscribed students
   */
  public int numberOfSubscribedStudents(){
	  return subscribedStudents.size();
  }

  /**
   * lists all currently subscribed students of this course
   * @return a String list of subscribed students
   */
  public String listStudents() {
    String list = new String();
    for (Student s : subscribedStudents) {
      list += s.toString();
    }
    return list;
  }

  /**
   * defines a properly formatted String for Course-objects
   * @return formated representation of a Course-object
   */
  public String toString() {
    return courseNr + "\t" + title + "\t\t" + subscribeStart.getTime().toString() + "\t" + 
    		subscribeEnd.getTime().toString() + "\t" + unsubscribeEnd.getTime().toString() + "\t" + 
    		numberOfSubscribedStudents() + "\n";
  }
}
